import json
f = open('user.json','r',encoding='utf-8')
data = json.load(f)
f.close()

print("欢迎来到xxx 用户系统，请输入您的操作指令")
print("0 >>> 退出")
print("1 >>> 注册")
print("2 >>> 登录")
print("3 >>> 修改密码")
print("4 >>> 删除用户")
while True:
    flag = input("请输入代码指令")
    if flag=='1':
        username = input("用户名")
        password = input("密码")
        if username in data:
            print("用户名已存在")
        elif len(username)<6 or len(username)>20:
            print("用户名长度错误")
        else:
            data[username] = password
            print("注册成功")
            print(data)
    elif flag=='2':
        username = input("用户名")
        password = input("密码")
        if username not in data:
            print("用户不存在")
        elif data[username] != password:
            print("密码输入错误")
        else:
            print("登录成功")

    elif flag == '3':
        username = input("用户名")
        password = input("密码")
        newpassword = input("新密码")
        if username not in data:
            print("用户不存在")
        elif data["username"] != password:
            print("密码输入错误")
        elif password==newpassword:
            print("新密码与就密码不能一样")
        else:
            data[username] = newpassword
            print("密码修改成功")

    elif flag == '4':
        username = input("用户名")
        password = input("密码")
        if username not in data:
            print("用户不存在")
        elif data[username] != password:
            print("密码输入错误")
        else:
            del data[username]
            print("删除成功")
    elif flag == '0':
        print('成功退出')
        break
    else:
        print("指令输入有误")

f = open('user.json','w',encoding='utf-8')
json.dump(data,f,ensure_ascii=False)
f.close()
